<?php
namespace Admin\Controller;
use Think\Controller;

class UploadifyController extends CommonController{
	public function index(){
		$this->display();
	}
	
	public function upload(){
        $config = array(
        'mimes'         =>  array(), //允许上传的文件MiMe类型
        'maxSize'       =>  5*1024*10240, //上传的文件大小限制 (0-不做限制)
        'exts'          =>  array('jpg','gif','png','jpeg','zip','rar','tar','gz','7z','doc','docx','txt','xml'), //允许上传的文件后缀
        'autoSub'       =>  true, //自动子目录保存文件
        'subName'       =>  array('date', 'Y-m-d'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
        'rootPath'      =>  './Uploads/', //保存根路径 默认值'./Uploads' index.php主入口文件下的uploads下
        'savePath'      =>  '/Picture/', //保存路径 默认为 '' 相对于rootPath  注意：savePath只是简单的拼接，并没有做处理，所以设置时务必采用'/Picture/'这种格式
        'saveName'      =>  array('uniqid', ''), //上传文件命名规则，[0]-函数名，[1]-参数，多个参数使用数组
        'saveExt'       =>  '', //文件保存后缀，空则使用原后缀
        'replace'       =>  false, //存在同名是否覆盖
        'hash'          =>  true, //是否生成hash编码
        'callback'      =>  false, //检测文件是否存在回调，如果存在返回文件信息数组
        //下面两项一般在实例化upload时设置 new Upload($setting, $driver='Local', $driverConfig);
        'driver'        =>  'Local', // 文件上传驱动 
        'driverConfig'  =>  array(), // 上传驱动配置  
        );
        
        //标准返回数据
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');

        if (!empty($_FILES)) {
            $upload = new \Think\Upload($config,$config['driver'],$config['driverConfig']); 
            if (!$info = $upload->upload()) {
                $return['status'] = 0;
				$return['info'] = $upload->getError();
			    echo json_encode($return); exit;
            } else {
                
                $fileInfo = $info['Filedata'];
                $rootPath = rtrim(ltrim($config['rootPath'],'.'),'/'); //result: '/Upload'
                $savePath = ltrim($fileInfo['savepath'].$fileInfo['savename'],'.'); //result: '/Picture/2014-10-17/54978e1adbbda.jpg'
                $path = $rootPath.$savePath;
                //状态信息
                $return['status'] = 1;
				$return['info'] = '上传成功';
                $return['data'] = array(
                    'name'=> $fileInfo['name'],
                    'path'    => $path
                );
                echo json_encode($return); exit;
            }
        }
	}

}
